package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.ChineseCartoonActivity;
import flc.ast.activity.DailyRecActivity;
import flc.ast.activity.KrCartoonActivity;
import flc.ast.activity.PopRankActivity;
import flc.ast.activity.SearchActivity;
import flc.ast.activity.TypeActivity;
import flc.ast.adapter.RecCaricatureAdapter;
import flc.ast.adapter.StrongRecAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private StrongRecAdapter strongRecAdapter;
    private RecCaricatureAdapter recCaricatureAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);

        mDataBinding.llDailyRec.setOnClickListener(this);
        mDataBinding.llPopRank.setOnClickListener(this);
        mDataBinding.llChineseCartoon.setOnClickListener(this);
        mDataBinding.llKrCartoon.setOnClickListener(this);
        mDataBinding.llAllType.setOnClickListener(this);

        mDataBinding.rvStrongRecList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        strongRecAdapter = new StrongRecAdapter();
        mDataBinding.rvStrongRecList.setAdapter(strongRecAdapter);
        strongRecAdapter.setOnItemClickListener(this);

        mDataBinding.rvRecCaricatureList.setLayoutManager(new GridLayoutManager(mContext, 3));
        recCaricatureAdapter = new RecCaricatureAdapter();
        mDataBinding.rvRecCaricatureList.setAdapter(recCaricatureAdapter);
        recCaricatureAdapter.setOnItemClickListener(this);

        //搜索
        mDataBinding.ivSearch.setOnClickListener(this);
        mDataBinding.etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                String key = mDataBinding.etSearch.getText().toString().trim();
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    if (key.isEmpty()) {
                        ToastUtils.showShort(getString(R.string.please_input_search));
                        return true;
                    }
                    SearchActivity.key = key;
                    startActivity(SearchActivity.class);
                }
                return false;
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSearch://搜素
                String key = mDataBinding.etSearch.getText().toString().trim();
                if (key.isEmpty()) {
                    ToastUtils.showShort(getString(R.string.please_input_search));
                    return;
                }
                SearchActivity.key = key;
                startActivity(SearchActivity.class);
                break;
            case R.id.llDailyRec://每日推荐
                startActivity(DailyRecActivity.class);
                break;
            case R.id.llPopRank://人气排行
                startActivity(PopRankActivity.class);
                break;
            case R.id.llChineseCartoon://国漫
                startActivity(ChineseCartoonActivity.class);
                break;
            case R.id.llKrCartoon://韩漫
                startActivity(KrCartoonActivity.class);
                break;
            case R.id.llAllType://全部分类
                startActivity(TypeActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        getStrongRecData();
        getRecCaricatureData();
    }

    private void getStrongRecData() {
        StkResApi.getTagResourceList(this, AppConfig.STRONG_DATA, StkResApi.createParamMap(0, 5), true, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        strongRecAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getRecCaricatureData() {
        StkResApi.getTagResourceList(this, AppConfig.REC_CARICATURE_DATA, StkResApi.createParamMap(0, 12), true, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        recCaricatureAdapter.setList(result);
                    }
                }
            }
        });
    }

}